<template>
	<div>
		<!--		查询-->
		<el-row>
			<el-col :span="24">
				<el-form :inline="true" label-width="auto" :model="state.query">
					<el-form-item label="沟通状态">
						<el-select
							v-model="state.query.isView"
							style="width: 140px"
							placeholder="沟通状态"
						>
							<el-option label="未沟通" value="0" />
							<el-option label="已沟通" value="1" />
						</el-select>
					</el-form-item>

					<el-form-item>
						<el-button type="primary" @click="init">查找</el-button>
					</el-form-item>
				</el-form>
			</el-col>
		</el-row>
		<!--		表单-->
		<el-row>
			<el-col :span="24">
				<el-table :data="state.itemList" stripe style="width: 100%">
					<el-table-column align="center" prop="id" label="编号" />
					<el-table-column align="center" prop="jobName" label="岗位名称" />
					<el-table-column align="center" prop="nickName" label="用户昵称" />
					<el-table-column align="center" prop="jobName" label="岗位名称" />
					<el-table-column align="center" prop="wchat" label="微信号" />
					<el-table-column align="center" prop="mobile" label="手机号" />
					<el-table-column align="center" prop="createTime" label="发起时间" />

					<el-table-column align="center" prop="isView" label="是否沟通">
						<template #default="scope">
							<el-tag v-if="scope.row.isView === 0" type="warning"
								>未沟通</el-tag
							>
							<el-tag v-if="scope.row.isView === 1" type="primary"
								>已沟通</el-tag
							>
						</template>
					</el-table-column>
					<el-table-column align="center" prop="viewInfo" label="回复内容" />
					<el-table-column
						align="center"
						fixed="right"
						label="操作"
						min-width="120"
					>
						<template #default="scope">
							<el-button
								link
								type="primary"
								size="small"
								@click.prevent="btn(scope.row)"
								>发起沟通</el-button
							>
							<el-button
								link
								type="primary"
								size="small"
								@click.prevent="del(scope.row.id)"
								>取消沟通</el-button
							>
						</template>
					</el-table-column>
				</el-table>
				<div class="page">
					<el-pagination
						background
						layout="prev, pager, next"
						:total="state.query.total"
						@current-change="sendValue"
					/>
				</div>
			</el-col>
		</el-row>
	</div>
	<el-dialog
		v-model="state.dialogVisible"
		:title="state.title"
		width="50%"
		:close-on-click-modal="false"
		:close-on-press-escape="false"
	>
		<el-form
			ref="ruleFormRef"
			:rules="rules"
			:model="state.from"
			label-width="auto"
			style="max-width: 600px"
		>
			<el-form-item label="沟通人昵称" prop="nickName">
				<el-input v-model="state.from.nickName" disabled />
			</el-form-item>
			<el-form-item label="沟通岗位" prop="jobName">
				<el-input v-model="state.from.jobName" disabled />
			</el-form-item>
			<el-form-item label="沟通人微信号" prop="wchat">
				<el-input v-model="state.from.wchat" disabled />
			</el-form-item>
			<el-form-item label="沟通人手机号" prop="mobile">
				<el-input v-model="state.from.mobile" disabled />
			</el-form-item>

			<el-form-item label="回复内容" prop="viewInfo">
				<el-input v-model="state.from.viewInfo" :rows="6" type="textarea" />
			</el-form-item>
		</el-form>
		<template #footer>
			<div class="dialog-footer">
				<el-button @click="update(ruleFormRef)">确定 回复</el-button>
			</div>
		</template>
	</el-dialog>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const ruleFormRef = ref<FormInstance>()
import type { FormInstance } from 'element-plus'
const rules = reactive({
	viewInfo: [
		{
			required: true,
			message: '回复不能为空',
			trigger: 'change',
		},
	],
})
const state = reactive({
	imgPath: '',
	dialogVisible: false,
	title: '',
	//条件查询
	query: {
		isView: '',
		page: 1,
		pagesize: 10,
		total: 10000,
	},
	itemList: [],
	from: {},
})
function btn(data: any) {
	state.from = data
	state.dialogVisible = true
}
function init() {
	state.imgPath = import.meta.env.VITE_API_BASE_URL + '/file/upload'
	http
		.get(`/feedback/page?page=${state.query.page}&isView=${state.query.isView}`)
		.then((res) => {
			state.itemList = res.data.list
			state.query.total = res.data.total
		})
}

//分页
/**
 * 分页出发函数
 * @param val
 */
const sendValue = (val: number) => {
	state.query.page = val
	init()
}

//修改
const update = async (formEl: FormInstance | undefined) => {
	if (!formEl) return
	await formEl.validate((valid) => {
		if (valid) {
			http.put('feedback/update', state.from).then(() => {
				toast.success('修改成功')
				state.dialogVisible = false
				state.from = {}
				init()
			})
		}
	})
}

//删除
function del(id: number) {
	http.delete(`user/${id}`).then(() => {
		toast.success('删除成功')
		init()
	})
}

init()
</script>

<style scoped>
.page {
	margin: 20px 0;
	display: flex;
	justify-content: center;
	text-align: center;
}
:deep(.avatar-uploader .el-upload) {
	border: 1px dashed var(--el-border-color);
	border-radius: 6px;
	cursor: pointer;
	position: relative;
	overflow: hidden;
	transition: var(--el-transition-duration-fast);
}
:deep(.avatar-uploader .el-upload:hover) {
	border-color: var(--el-color-primary);
}
:deep(.el-icon.avatar-uploader-icon) {
	font-size: 28px;
	color: #8c939d;
	width: 178px;
	height: 178px;
	text-align: center;
}
.avatar {
	width: 178px;
	height: 178px;
}
</style>
<route lang="json">
{
	"meta": {
		"title": "后台",
		"layout": "admin"
	}
}
</route>
